package com.xuzhijie.pms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xuzhijie.pms.common.Constants;
import com.xuzhijie.pms.common.Result;
import com.xuzhijie.pms.entity.Visitor;
import com.xuzhijie.pms.exception.PmsServerException;
import com.xuzhijie.pms.mapper.VisitorMapper;
import com.xuzhijie.pms.service.IVisitorService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 许志杰
 * @since 2024-02-17
 */
@Service
public class VisitorServiceImpl extends ServiceImpl<VisitorMapper, Visitor> implements IVisitorService {



    @Override
    public Result findPage(Integer page, Integer size, Integer status) {
        Page<Visitor> visitorPage = new Page<>(page,size);
        QueryWrapper<Visitor> queryWrapper = new QueryWrapper<>();
        Page<Visitor> result;
        Long total;

        if (!status.equals(0)){
            queryWrapper.eq("status",status);
        }


        try {
            result = page(visitorPage,queryWrapper);
            total = count(queryWrapper);
        }catch (Exception e){
            e.printStackTrace();
            throw new PmsServerException("业务繁忙", Constants.SYSTEM_ERROR);
        }


        Map<String,Object> resultMap = new HashMap<>();
        resultMap.put("records",result.getRecords());
        resultMap.put("total",total);

        return Result.success(resultMap);
    }

    @Override
    public Result addVisitor(String description, String address, String owner, String nickname, String type, String status) {
        Visitor v = new Visitor();
        v.setDescription(description);
        v.setVisitorSalutation(nickname);
        v.setAccessApplications(owner);
        v.setStatus(Integer.parseInt(status));
        v.setPlacesToVisit(address);

        save(v);
        return Result.success();
    }

    @Override
    public Result getVisitor(String owner) {
        QueryWrapper<Visitor> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("access_applications",owner);
        queryWrapper.eq("status",1);
        List<Visitor> list = list(queryWrapper);
        return Result.success(list);
    }

}
